package dao.impl;

import bean.supplier;
import bean.wareHouse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.supplierDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.c3p0Utils;

import java.sql.SQLException;
import java.util.List;

public class supplierImpl implements supplierDao {
    @Override
    public int supplierAdd(String suppliername, String supplierphone, String supplieradd) {
        String sql="INSERT INTO Supplier (suppliername,supplierphone,supplieradd,isalive) VALUES (?,?,?,1)";
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=qr.update(sql,suppliername,supplierphone,supplieradd);//
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int supplierDel(int supplierId) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=qr.update("update Supplier set isalive=? where supplierId = ?",0,supplierId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int supplierUpd(String supplieradd,String supplierphone,int supplierId){
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=qr.update("UPDATE Supplier SET supplierphone=?,supplieradd=? WHERE supplierId=?",supplierphone,supplieradd,supplierId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public supplier supplierSignal(int supplierId) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        supplier su=null;
        try {
            su=qr.query("select * from Supplier where supplierId=? and isalive=?",new BeanHandler<supplier>(supplier.class),supplierId,1);//
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return su;
    }

    @Override
    public PageInfo suList(int page, int size) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        PageHelper.startPage(page,size);
        List<supplier> list=null;
        try {
            list=qr.query("select * from Supplier where isalive=?",new BeanListHandler<supplier>(supplier.class),1);//
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageInfo<supplier> pageinfo=new PageInfo<>(list);

        return pageinfo;
    }

    @Override
    public PageInfo supplierOne(String suppliername) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        List<supplier> suOne= null;
        try {
            suOne=qr.query("select * from Supplier where suppliername=? and isalive=?",new BeanListHandler<supplier>(supplier.class),suppliername,1);//

        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageInfo<supplier> pageinfo=new PageInfo<>(suOne);
        return pageinfo;
    }
}
